package com.nfzm.tools;

import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

@Slf4j
public class HttpTool {

    public static JSONObject get(String url ){

        HttpResponse response ;
        JSONObject result=null;
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet get = new HttpGet(url);
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(50000) // 连接超时时间，单位毫秒
                .setSocketTimeout(50000)  // 读取超时时间，单位毫秒
                .build();
        get.setConfig(requestConfig);
        //get.setHeader("Connection","keep-alive");

       /* String jsonString =entity.toString();
        try {
            StringEntity stringEntity = new StringEntity(jsonString);
        } catch (UnsupportedEncodingException e) {
            log.error("请求体编码错误"+e.getMessage());
            return result;
        }*/
        //stringEntity.setContentType("application/json");
        //post.setEntity(stringEntity);
        //post.setEntity(new StringEntity(entity, ContentType.APPLICATION_JSON));


        try{
            response = httpclient.execute(get);
            String body = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
            result = JSONObject.parseObject(body);
            //System.out.println(result);

        }catch (Exception e){
            log.error("请求接口出错",e);

        }


        return result;

    }

}
